module VirtualCityScripts.Bus

open Casanova
open Casanova.Core
open Casanova.Coroutines
open Casanova.Game
open Casanova.Input
open Casanova.Math
open Casanova.Drawing
open VirtualCity.City
open VirtualCity.CityDescription

let do_rounds(bus:Bus,city:City) = 
    co{
      for s in bus.Stops do
        do bus.Physics.Target := s.Location
        let clock = city.Clock
        do! wait_condition(fun () -> !clock.Time >= s.Time)
        do city.Agents :=
             [
               yield! !city.Agents
               for p in !bus.Passengers do 
                 if !p.Destination = s then
                   yield p.Agent
             ]
        do bus.Passengers := 
             [
               for p in !bus.Passengers do 
                 if !p.Destination <> s then
                   yield p
               for w in !s.BusWaiters do
                 if w.BusNumber = bus.Number then
                   yield  {
                            Agent       = w.Agent
                            Destination = w.Destination
                          }
             ]
        do s.BusWaiters := 
             [
               for w in !s.BusWaiters do
                 if w.BusNumber <> bus.Number then
                   yield w
             ]
    } |> repeat_
